package DB_Layer;

import static org.junit.Assert.*;

import java.util.ArrayList;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import Model_Layer.Customer;
import Model_Layer.Zipcode_City;

@SuppressWarnings("unused")
public class DB_CustomerTest {
	private DB_Customer dbCusObj;

	@Before
	public void setUp() throws Exception {
		dbCusObj = new DB_Customer();
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testInsertCustomer() {
		
		Customer newTestCus = new Customer();
		
		// setting up customer
		// cusID will be set up in DB_Customer
		newTestCus.setCusID(0);
		newTestCus.setName("Test kunde");
		newTestCus.setAddress("Testgade 1234");
		newTestCus.setPhoneNo("98765432");
		newTestCus.setCity("Noerresundby");
		newTestCus.setEmail("Test@testtest.dk");
		newTestCus.setPrivate_Business('b');
		newTestCus.setCountry("Testland");
		newTestCus.setDiscount(0);
		newTestCus.setZipCode(9400);
		
		if(newTestCus != null) {
			try {
				dbCusObj.insertCustomer(newTestCus);
			}
			catch(Exception e) {
				fail("Customer not inserted");
			}
		}
	}

	@Test
	public void testUpdateCustomer() {
		Customer newUpdateCus = new Customer();
		
		// find latest id 
		int cusID = GetMaxID.getMaxID("SELECT max(cusID) from Customer ");
		
		newUpdateCus.setCusID(cusID);			
		newUpdateCus.setName("Test kunde 2");
		newUpdateCus.setAddress("Testgade 1234");
		newUpdateCus.setPhoneNo("98765432");
		newUpdateCus.setCity("Herning");
		newUpdateCus.setEmail("Test@testtest.dk");
		newUpdateCus.setPrivate_Business('b');
		newUpdateCus.setCountry("Testland");
		newUpdateCus.setDiscount(0);
		newUpdateCus.setZipCode(7400);
		
		dbCusObj.updateCustomer(newUpdateCus);
	}
	
	
	@Test
	public void testGetCustomer() {
		System.out.println("Test af getcustomer");
		int cusID = GetMaxID.getMaxID("SELECT max(cusID) from Customer");
		Customer foundcus = dbCusObj.getCustomer(cusID, true);
		System.out.println("Kunden med id: " + cusID + " har navnet: " + foundcus.getName());
	}

	@Test
	public void testDeleteCustomer() {
		
		// get latest id for deleting
		int CusID = GetMaxID.getMaxID("SELECT max(cusID) from Customer");
		
		try {
			dbCusObj.deleteCustomer(CusID);
			System.out.println("Customer with id: " + CusID + " was deleted!");
		}
		catch(Exception e){
			fail("Customer not deleted");
		}	
	}

	@Test
	public void testGetAllCustomers() {
		
		ArrayList<Customer> allCustomersList = dbCusObj.getAllCustomers();
		
		for(Customer cus : allCustomersList) {
			System.out.println("Kundenummer: " + cus.getCusID() + "| Kundenavn: " + cus.getName() +
								"| Addresse: " + cus.getAddress());
		}
	}
	

	

}
